import json
import argparse
import sys
from pathlib import Path

def remove_first_six_chars(filename):
    # 读取文件内容
    with open(filename, 'r') as file:
        content = file.read()
        print(content)
    
    # 删除前6个字符（如果内容不足6个字符则返回空字符串）
    modified_content = content[6:]
    
    # 将修改后的内容写回文件
    with open(filename, 'w') as file:
        file.write(modified_content)

def format_json_file(input_path: str, output_path: str = None, indent: int = 4) -> None:
    """
    格式化 JSON 文件
    :param input_path: 输入文件路径
    :param output_path: 输出文件路径（默认覆盖原文件）
    :param indent: 缩进空格数
    """
    # 处理默认输出路径
    if output_path is None:
        output_path = input_path
        
    remove_first_six_chars(input_path)    

    try:
        # 读取文件
        with open(input_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
        
        # 写入格式化后的内容
        with open(output_path, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=indent, sort_keys=True)
        
        print(f"✅ 格式化完成：{Path(output_path).resolve()}")

    except json.JSONDecodeError as e:
        print(f"❌ JSON 格式错误：{e}")
    except FileNotFoundError:
        print(f"❌ 文件不存在：{input_path}")
    except PermissionError:
        print(f"❌ 无写入权限：{output_path}")
    except Exception as e:
        print(f"❌ 未知错误：{str(e)}")

if __name__ == "__main__":
    # 命令行参数配置
    file_path = sys.argv[1]
    
    
    # 执行格式化
    format_json_file(
        input_path=file_path+"_cut.json",
        output_path=file_path+"_fixed_tmp.json",
        indent=4
    )
